#include "livewebentrydao.h"
#include <QDebug>
#include <QSqlError>
#include <QSqlQuery>
#include <QVariant>

LiveWebEntryDAO::LiveWebEntryDAO(QSqlDatabase& db) : m_db(db) { }

bool LiveWebEntryDAO::createTable()
{
    QSqlQuery query(m_db);
    QString createTableSql = "CREATE TABLE IF NOT EXISTS live_entries2 ("
                             "platform INTEGER PRIMARY KEY, "
                             "name TEXT, "
                             "url TEXT, "
                             "profile_url TEXT, "
                             "ua INTEGER, "
                             "injection_point INTEGER, "
                             "source_code TEXT, "
                             "version INTEGER)";

    if (!query.exec(createTableSql)) {
        qWarning() << "Failed to create live_entries2 table:" << query.lastError().text();
        return false;
    }

    return true;
}

bool LiveWebEntryDAO::insert(const yc::LiveWebEntry& entry)
{
    QSqlQuery query(m_db);
    query.prepare("INSERT OR REPLACE INTO live_entries2 "
                  "(platform, name, url, profile_url, ua, injection_point, source_code, version) "
                  "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");

    query.addBindValue(entry.platform);
    query.addBindValue(entry.name);
    query.addBindValue(entry.url);
    query.addBindValue(entry.profile_url);
    query.addBindValue(entry.ua);
    query.addBindValue(entry.script.injection_point);
    query.addBindValue(entry.script.source_code);
    query.addBindValue(entry.version);

    if (!query.exec()) {
        qWarning() << "Failed to insert yc::LiveWebEntry:" << query.lastError().text();
        return false;
    }

    return true;
}

bool LiveWebEntryDAO::update(const yc::LiveWebEntry& entry)
{
    QSqlQuery query(m_db);
    query.prepare("UPDATE live_entries2 SET "
                  "name = ?, url = ?, profile_url = ?, ua = ?, injection_point = ?, "
                  "source_code = ?, version = ? WHERE platform = ?");

    query.addBindValue(entry.name);
    query.addBindValue(entry.url);
    query.addBindValue(entry.profile_url);
    query.addBindValue(entry.ua);
    query.addBindValue(entry.script.injection_point);
    query.addBindValue(entry.script.source_code);
    query.addBindValue(entry.version);
    query.addBindValue(entry.platform);

    if (!query.exec()) {
        qWarning() << "Failed to update yc::LiveWebEntry:" << query.lastError().text();
        return false;
    }

    return query.numRowsAffected() > 0;
}

bool LiveWebEntryDAO::remove(int platform)
{
    QSqlQuery query(m_db);
    query.prepare("DELETE FROM live_entries2 WHERE platform = ?");
    query.addBindValue(platform);

    if (!query.exec()) {
        qWarning() << "Failed to remove yc::LiveWebEntry:" << query.lastError().text();
        return false;
    }

    return query.numRowsAffected() > 0;
}

yc::LiveWebEntry LiveWebEntryDAO::getByPlatform(int platform)
{
    QSqlQuery query(m_db);
    query.prepare("SELECT * FROM live_entries2 WHERE platform = ?");
    query.addBindValue(platform);

    yc::LiveWebEntry entry;
    entry.platform = yc::UNKNOWN_PLATFORM_TYPE;

    if (query.exec() && query.next()) {
        entry.platform = query.value("platform").toInt();
        entry.name = query.value("name").toString();
        entry.url = query.value("url").toString();
        entry.profile_url = query.value("profile_url").toString();
        entry.ua = query.value("ua").toInt();
        entry.script.injection_point = query.value("injection_point").toInt();
        entry.script.source_code = query.value("source_code").toString();
        entry.version = query.value("version").toInt();
    } else {
        qWarning() << "No LiveWebEntry found for platform:" << platform;
    }

    return entry;
}

int LiveWebEntryDAO::getVersionByPlatform(int platform)
{
    int version = -1;

    QSqlQuery query(m_db);
    query.prepare("SELECT version FROM live_entries2 WHERE platform = ?");
    query.addBindValue(platform);

    if (query.exec() && query.next()) {
        version = query.value("version").toInt();
    } else {
        qWarning() << "No LiveWebEntry found for platform:" << platform;
    }

    return version;
}

QVector<yc::LiveWebEntry> LiveWebEntryDAO::getAll()
{
    QVector<yc::LiveWebEntry> entries;
    QSqlQuery query("SELECT * FROM live_entries2", m_db);

    if (query.exec()) {
        while (query.next()) {
            yc::LiveWebEntry entry;
            entry.platform = query.value("platform").toInt();
            entry.url = query.value("url").toString();
            entry.profile_url = query.value("profile_url").toString();
            entry.ua = query.value("ua").toInt();
            entry.script.injection_point = query.value("script_injection_point").toInt();
            entry.script.source_code = query.value("script_source_code").toString();
            entry.version = query.value("version").toInt();

            entries.append(entry);
        }
    } else {
        qWarning() << "Failed to get all LiveWebEntries:" << query.lastError().text();
    }

    return entries;
}
